//
//  AISUISlider.m
//  AISHomeCare
//
//  Created by Ares on 10/11/15.
//  Copyright © 2015 Ares. All rights reserved.
//

#import "AISUISlider.h"

@interface AISUISlider() {
    NSTimer *checkValueTimer;
    float currentSliderValue;
    BOOL isDragging;
}
@end


@implementation AISUISlider

- (id)init {
    self = [super init];
    if (nil != self) {
        [self addDragAction];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (nil != self) {
        [self addDragAction];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (nil != self) {
        [self addDragAction];
    }
    return self;
}

#pragma mark Property

- (void)setVertical:(BOOL)vertical {
    if (vertical) {
        [self setTranslatesAutoresizingMaskIntoConstraints:YES];
        [self setTransform:CGAffineTransformMakeRotation(- M_PI_2)];
    }
}

- (void)setCsThumbImage:(UIImage *)csThumbImage {
    _csThumbImage = csThumbImage;
    if (nil != csThumbImage) {
        [self setThumbImage:csThumbImage forState:UIControlStateNormal];
    }
}

- (void)setCsMinimumTrackImage:(UIImage *)csMinimumTrackImage {
    _csMinimumTrackImage = csMinimumTrackImage;
    if (nil != csMinimumTrackImage) {
        [self setMinimumTrackImage:csMinimumTrackImage forState:UIControlStateNormal];
    }
}

- (void)setCsMaximumTrackImage:(UIImage *)csMaximumTrackImage {
    _csMaximumTrackImage = csMaximumTrackImage;
    if (nil != csMaximumTrackImage) {
        [self setMaximumTrackImage:csMaximumTrackImage forState:UIControlStateNormal];
    }
}

- (BOOL)isDragging {
    return isDragging;
}

#pragma mark

- (void)addDragAction {
    [super addTarget:self
              action:@selector(sliderBeginDrag:)
    forControlEvents:(UIControlEventTouchDown)];
    [super addTarget:self
              action:@selector(sliderExitDrag:)
    forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
}

- (void)sliderBeginDrag:(id)sender {
    isDragging = YES;
    currentSliderValue = self.value;
    if (nil != checkValueTimer) {
        [checkValueTimer invalidate];
        checkValueTimer = nil;
    }
    checkValueTimer = [NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(checkSliderValueChanges:) userInfo:nil repeats:YES];
}

- (void)sliderExitDrag:(id)sender {
    isDragging = NO;
    [self checkSliderValueChanges:nil];
    
    [checkValueTimer invalidate];
    checkValueTimer = nil;
}

- (void)checkSliderValueChanges:(NSTimer *)timer {
    if (currentSliderValue != self.value) {
        // TODO
        if ([self.delegate respondsToSelector:@selector(sliderValueChanged:)]) {
            [self.delegate sliderValueChanged:self];
        }
        currentSliderValue = self.value;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
